import gtk
import gobject

import DITrack.DB.Common
import DITrack.DB.Exceptions
import DITrack.Client
import DITrack.dt.globals
import DITrack.Command.generic

def err (str):
  print (str)

class DTIssue:
  def __init__ (self, p):
    self.props = p

  def get (self, key):
    return self.props[key]

class DT:
  def __init__ (self, d):
    self.dir = d
    self.list = gtk.ListStore(gobject.TYPE_PYOBJECT)
    self.globals = DITrack.dt.globals.Globals()
    self.globals.username = "jcvallejo"
    self.db = self.open_db ();

  def update (self):
    self.list.append ([DTIssue ({'id': 0, 'owner' : "jcvallejo", 'version' : "2.1", 'status' : "open", 'title' : "Una incidencia"})])

  def open_db (self):
    db = None
    try:
        db = DITrack.DB.Common.Database(self.dir, self.globals.username, self.globals.svn_path, "w")
    except DITrack.DB.Exceptions.NotDatabaseError, e:
        err("'" + self.dir + "' is not an issue database root")
    except DITrack.DB.Exceptions.NotDirectoryError, e:
        err("'" + self.dir + "' doesn't exist or is not a "
            "directory")
    except DITrack.DB.Exceptions.InvalidVersionError, e:
        err("'" + self.dir + "' is not of a supported database "
            "format version")
    except DITrack.DB.Exceptions.InvalidUserError, e:
        err("Invalid user name: '%s'" % self.globals.username)
    except DITrack.DB.Exceptions.DBIsLockedError, e:
        err("Database '" + self.dir + "' is locked")
    except DITrack.DB.Exceptions.CorruptedDB_UnparseableListingFormatError, e:
        err(e)
    except:
        raise
    return db

  def add (self):
    self.list

  def get_list (self):
    self.list.clear ()
    client = DITrack.Client.Client(self.db)
    for id, issue in client.issues([]):
      self.list.append ([DTIssue (issue.info)]);

    return self.list
